##
#   Project: gmediacatalog - Catalog your media to find them later
#    Author: Fabio Castelli <muflone@vbsimple.net>
# Copyright: 2010 Fabio Castelli
#   License: GPL-2+
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the Free
#  Software Foundation; either version 2 of the License, or (at your option)
#  any later version.
# 
#  This program is distributed in the hope that it will be useful, but WITHOUT
#  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
#  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
#  more details.
# 
# On Debian GNU/Linux systems, the full text of the GNU General Public License
# can be found in the file /usr/share/common-licenses/GPL-2.
##

def get_file_modes(mode):
  "Decode octal modes"
  if mode & 077630000:
    print mode, mode & 077630000
  return '%s%s%s%s%s%s%s%s%s%s' % (
     (mode & 040000) and 'd' or '-',                            # directory
     (mode & 000400) and 'r' or '-',                            # user r
     (mode & 000200) and 'w' or '-',                            # user w
     (mode & 004000) and ((mode & 00100) and 's' or 'S') or     # suid s/S
    ((mode & 000100) and 'x' or '-'),                           # user x
     (mode & 000040) and 'r' or '-',                            # group r
     (mode & 000020) and 'w' or '-',                            # group w
     (mode & 002000) and ((mode & 00010) and 's' or 'S') or     # sgid s/S
    ((mode & 000010) and 'x' or '-'),                           # group x
     (mode & 000004) and 'r' or '-',                            # other r
     (mode & 000002) and 'w' or '-',                            # other w
     (mode & 001000) and ((mode & 00001) and 't' or 'T') or     # sticky t/T
    ((mode & 000001) and 'x' or '-')                            # other x
  )
